package com.esg.esgproject.mp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.esg.esgproject.mp.entity.auto.Company;
import com.esg.esgproject.mp.entity.auto.News;
import com.esg.esgproject.mp.qo.QoCompany;
import com.esg.esgproject.mp.qo.QoNews;
import com.esg.esgproject.mp.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author z100
 * @since 2021-12-15
 */
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    CompanyService companyService;

    @PutMapping("/add")
    public ResponseEntity<Object> add(Company company){
        boolean res = companyService.save(company);
        return res?new ResponseEntity<>(HttpStatus.OK):new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);
    }

    @DeleteMapping("/delete")
    public ResponseEntity<Object> delete(@RequestParam("id") Long id){
        boolean res = companyService.removeById(id);
        return res?new ResponseEntity<>(HttpStatus.OK):new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);
    }

    @GetMapping("/getById")
    public ResponseEntity<Object> getById(Integer id){
        LambdaQueryWrapper<Company> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(Company::getId,id);
        Company company = companyService.getOne(wrapper);
        return new ResponseEntity<>(company, HttpStatus.OK);
    }


    @GetMapping("/get")
    public ResponseEntity<Object> search(QoCompany company){
        LambdaQueryWrapper<Company> wrapper= new LambdaQueryWrapper<>();
        System.out.println("接收到的CompanyId:"+company.getId());
        wrapper.eq(Company::getId,company.getId());
        List<Company> list = companyService.list(wrapper);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @GetMapping("/getAll")
    public ResponseEntity<Object> getAll(){
        List<Company> all = companyService.list();
        return new ResponseEntity<>(all, HttpStatus.OK);
    }

    @GetMapping("/update")
    public ResponseEntity<Object> update(Company company,Long oldId){
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Company::getId,oldId);
        boolean res = companyService.update(company, wrapper);
        return res?new ResponseEntity<>(HttpStatus.OK):new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);
    }

}
